#include <avr/io.h>  		//includes instruction library for the ATMega32 MCU
#include <util/delay.h>    	//includes instruction library for time delays/pauses
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sonar.h"
#include "serial.h"

uint16_t volatile SonarReturnTime=0;					// Grab variable from external sonar.h function
uint8_t volatile InPulse=0;							// Declare variable from external sonar.h function
int volatile update=0;

ISR(SIG_OUTPUT_COMPARE0) {
	update++;
	if(update==250) {
		printf("\033[2J\033[H");						// Return cursor to home, clear screen
		printf("Time taken: %u\n\r",SonarReturnTime);	// Display latest 
		PulseSonar();									// Run function from sonar.h
		update=0;
	}
}

void init_ticker(void) {
	TCNT0=0;
	OCR0=250;
	TCCR0=_BV(WGM01)|_BV(CS01)|_BV(CS00);		// CTC on OCR0, clock/64
	TIMSK|=_BV(OCIE0);
}

int main(void) {
	DDRD|=0x80;
	PORTD|=0x20;
	init_uart();								// From serial.h
	init_ticker();
	sei();
	PulseSonar();
	while(1) {

	}
}
